package se.webbzon.boltzmann.factory;

import java.util.Collection;
import java.util.HashMap;

public abstract class AbstractFactoryGroup<K,F extends AbstractFactory<K,?,?>> {
	
	// A map containing all the factories in this group
	private final HashMap<K,F> factories = new HashMap<K,F>(); 
	
	/*============================================================
	Constructors
	============================================================*/
	
	public AbstractFactoryGroup(Collection<F> factories) 
			throws AbstractFactoryException {
		for (F factory : factories) {
			if (this.factories.put(factory.getKey(), factory) != null)
				throw new AbstractFactoryException(	"Key collision occurred " +
													"when creating abstract " +
													"factory. The key " +
													factory.getKey() +
													"is already added!");
		}
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Returns the factory which is associated with the given key. 
	 * If no factory is associated with the given key then null is
	 * returned. **/
	public F getFactory(K key) {
		return factories.get(key);
	}
	
	/** Returns a collection of all the factories in this factory group. **/
	public Collection<F> getFactories() {
		return factories.values();
	}

}
